package com.cheegu.common.parser.gpsdata.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author tac
 * @since 21/03/2018
 */
public abstract class CodingUtils {
    /**
     * hex2bytes十六进制字符串转换为字节数组
     * @param [hex]
     * @return byte[]
     */
    public static byte[] hex2bytes(String hex) {
        if (hex == null || hex.trim().length() == 0) {
            throw new IllegalArgumentException("hex");
        }

        if (hex.length() % 2 != 0) {
            hex += "20";
        }

        byte[] bytes = new byte[hex.length() / 2];

        for (int i = 0; i < bytes.length; i++) {
            //每两个字符是一个 byte。
            int currentIndex = i * 2;
            String hexStr = hex.substring(currentIndex, currentIndex + 2);
            try {
                bytes[i] = (byte) Integer.parseInt(hexStr, 16);
            } catch (Exception e) {
                throw new IllegalArgumentException(String.format("%s is not a valid hex number!", hexStr));
            }
        }
        return bytes;
    }
    /**
     * printHexString将指定byte数组以16进制的形式打印到控制台
     * @param [b]
     * @return String
     */
    public static String bytesToHexString(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            String hv = Integer.toHexString(src[i] & 0xFF).toUpperCase();
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
    /**
     * pseudoIPToCode将字节数组转换成设备号
     * @param [buffer]
     * @return java.lang.String
     */
    public static String pseudoIPToCode(byte[] buffer) {
        byte[] dataBuffer = new byte[4];
        System.arraycopy(buffer, 0, dataBuffer, 0, dataBuffer.length);
        int numberSegment = 130;
        byte v1 = dataBuffer[0];
        byte v2 = dataBuffer[1];
        byte v3 = dataBuffer[2];
        byte v4 = dataBuffer[3];
        if (v1 > 0x80) {
            v1 = (byte) (v1 - 0x80);
            numberSegment = numberSegment + 8;
        }
        if (v2 >= 0x80) {
            v2 = (byte) (v2 - 0x80);
            numberSegment = numberSegment + 4;
        }
        if (v3 >= 0x80) {
            v3 = (byte) (v3 - 0x80);
            numberSegment = numberSegment + 2;
        }
        if (v4 >= 0x80) {
            v4 = (byte) (v4 - 0x80);
            numberSegment = numberSegment + 1;
        }
        if (numberSegment == 142) {
            numberSegment = 158;
        }
        if (numberSegment == 143) {
            numberSegment = 159;
        }
        return Integer.toString(numberSegment) + String.format("%02d", v1) + String.format("%02d", v2) + String.format("%02d", v3) + String.format("%02d", v4);
    }
    public static Date formatStringToDate(String source,String format)
    {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {

            // SimpleDateFormat的parse(String time)方法将String转换为Date
            return  simpleDateFormat.parse(source);
        }
        catch (Exception ex)
        {
            return new Date(1970,1,1);
        }

    }
}
